import { Injectable } from "@angular/core";
import { BaseService, httpOptions } from "../../../core/service/base.service";
import { _HttpClient } from "@delon/theme";
import { Observable } from "rxjs";
import { catchError, tap } from "rxjs/operators";

@Injectable({
  providedIn: 'root'
})
export class UserService extends BaseService {

  getUrl(): string {
    return "api/sys-user";
  }

  constructor(http: _HttpClient) {
    super(http);
  }

  /**
   * 获取当前用户
   * @param id
   */
  getCurrentUser(): Observable<any> {
    const url = `${this.getUrl()}/get-current-user`;
    return this.http.get<any>(url).pipe(
      tap(_ => this.log(`fetched object `)),
      catchError(this.handleError<any>(`getany `)),
    );
  }

  /**
   * 更新用户信息
   * @param object
   */
  updateInfo(object: any): Observable<any> {
    return this.http.post<any>(this.getUrl() + "/update-info", object, httpOptions).pipe(
      tap((obj: any) => this.log(`added object =${obj}`)),
      catchError(this.handleError<any>('addany')),
    );
  }
}
